package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.AirportRepository;
import com.css.fxfzypg.modules.baseData.entity.Airport;
import com.css.fxfzypg.modules.baseData.service.AirportService;
import com.css.fxfzypg.modules.baseData.queryParams.AirportQueryParams;
import com.css.fxfzypg.modules.baseData.vo.AirportVO;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class AirportServiceImpl implements AirportService {

    @Resource
    AirportRepository airportRepository;

//    @Override
//    public AirportVO updateAirport(RoadVO roadVO) {
//        Road road = roadVO.getRoad();
//        Road road2 = roadRepository.updateRoadById(road);
//
//        return new RoadVO(road2);
//    }

    @Override
    public AirportVO getAirportById(Integer id) {
        Airport airport = airportRepository.getAirportById(id);
        return new AirportVO(airport);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAirport(List<Integer> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        airportRepository.deleteAirportsById(ids,timestamp);
    }

    @Override
    @Transactional
    public Airport saveAirport(Airport airport) {
        airport.setAirportStatus(YNEnum.Y.toString());
        return airportRepository.saveAirport(airport);
    }

    @Override
    public JSONObject getAllAirport(AirportQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(airportRepository.getAllAirportParams(queryParams));
    }

    @Override
    public List<Airport> getAll() {

        return airportRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<Airport> airports = FileUtil.importExcel(file, Airport.class);
        for(int i = 0 ; i<airports.size();i++){

            airports.get(i).setAirportStatus(YNEnum.Y.toString());

        }
        airportRepository.importExcel(airports);
    }
}
